#include "enmodule.h"
#include <QReadLocker>
#include <QDebug>

EnModule::EnModule( QString uName )
{
    userName = uName;
    backgroundFileName = ":/images/clouds.jpg";
    backgroundImage.load( backgroundFileName );
    backgroundBrush = new QBrush();
    backgroundBrush->setTextureImage( this->getBackgroundImage() );
}

EnModule::~EnModule()
{

}

QString EnModule::getBackgroundFileName()
{
    return backgroundFileName;
}

void EnModule::setBackgroundPixmap ( QGraphicsPixmapItem* p )
{
    this->backgroundPixmap = p;
}

QGraphicsPixmapItem* EnModule::getBackgroundPixmap()
{
    return backgroundPixmap;
}

void EnModule::setBackgroundFilename( QString b )
{
    this->backgroundFileName = b;
}

QBrush* EnModule::getBackgroundBrush()
{
    return backgroundBrush;
}


const QImage EnModule::getBackgroundImage()
{
    return backgroundImage;
}

void EnModule::setBackground( QString fileName )
{
    backgroundFileName = fileName;
    backgroundImage.load( backgroundFileName );
    backgroundBrush->setTextureImage( this->getBackgroundImage() );
}

void EnModule::localAddBody()
{
    EnBody* body = new EnBody( userName+QString::number( EnBody::localObjectCounter++ ) );
    bodyMap.insert( body->getMapKey(), body );
    //qDebug() << bodyMap.value( body->getMapKey() )->getMapKey();
    emit( bodyLocallyAdded( body ) );
}

void EnModule::localRemoveBody( QGraphicsScene* scene, QString bodyMapKey )
{
    //qDebug() << this->bodyMap.size();

    if( bodyMap.contains( bodyMapKey ) )
    {
        EnBody* body = bodyMap.value( bodyMapKey );
        scene->removeItem( body->getGraphicsItem() );
        //delete body->getGraphicsItem();
        bodyMap.remove( bodyMapKey );
        delete body;
        emit bodyLocallyRemoved( bodyMapKey );
    }

    else
    {
        qDebug() << "bodymap doesn't contain referenced key";
    }

}
